﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLPKWeb.Models.LinQ.DanhMucChung;
using Webdiyer.WebControls.Mvc;
using DONGACS.Common;
using QLPKWeb.Models.FormBean.DanhMucChungModel;
namespace QLPKWeb.BussinessDelegate.DanhMucChung
{
    public class KhachHangDelegate
    {
        DBDanhMucChungDataContext DBDanhMucChung = new DBDanhMucChungDataContext();

        public IList<dm_KhachHang> GetAllKhachHang()
        {
            return DBDanhMucChung.dm_KhachHangs.ToList();
        }
        public dm_KhachHang AddNewKhachHang(dm_KhachHang KhachHang)
        {
            DBDanhMucChung.dm_KhachHangs.InsertOnSubmit(KhachHang);
            DBDanhMucChung.SubmitChanges();
            return GetKhachHangIdMax();
        }
        public void DeleteKhachHang(string KhachHangID)
        {
            dm_KhachHang KhachHangNew =
                DBDanhMucChung.dm_KhachHangs.Single(p => p.Id == Convert.ToInt32(KhachHangID));
            DBDanhMucChung.dm_KhachHangs.DeleteOnSubmit(KhachHangNew);
            DBDanhMucChung.SubmitChanges();
        }
        public void UpdateKhachHang(dm_KhachHang KhachHang)
        {
            dm_KhachHang KhachHangUpdate =
                DBDanhMucChung.dm_KhachHangs.Single(p => p.Id == KhachHang.Id);
            KhachHangUpdate.Id = KhachHang.Id;
            KhachHangUpdate.TenKH = KhachHang.TenKH;
            KhachHangUpdate.DiaChi = KhachHang.DiaChi;
            KhachHangUpdate.SoDT = KhachHang.SoDT;
            KhachHangUpdate.Fax = KhachHang.Fax;
            KhachHangUpdate.Email = KhachHang.Email;
            KhachHangUpdate.WebSite = KhachHang.WebSite;
            KhachHangUpdate.MaST = KhachHang.MaST;
            KhachHangUpdate.IdLoaiKhachHang = KhachHang.IdLoaiKhachHang;
            DBDanhMucChung.SubmitChanges();
        }

        public PagedList<dm_KhachHang> SearchKhachHangPagedList(int? id, string searchCriteria)// id = page Index
        {
            //IQueryable<dm_KhachHang> list = DBDanhMucChung.dm_KhachHangs.Where(p => p.Ten == searchCriteria);
            //DBDanhMucChung.dm_KhachHangs.ToPagedList(list,)

            return (PagedList<dm_KhachHang>)DBDanhMucChung.dm_KhachHangs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage).Where(p => p.TenKH == searchCriteria);

        }

        public dm_KhachHang GetKhachHangById(string id)
        {
            try
            {
                dm_KhachHang item = this.DBDanhMucChung.dm_KhachHangs.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_KhachHang GetKhachHangByTen(string ten)
        {
            //dm_KhachHang item = this.DBDanhMucChung.dm_KhachHangs.Where(p => p.Ten == ten).Count() != 0 ? this.DBDanhMucChung.dm_KhachHangs.Where(p => p.Ten == ten).ElementAt(0) : null ;

            IEnumerable<dm_KhachHang> list = this.DBDanhMucChung.dm_KhachHangs.Where(p => p.TenKH == ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }

        public PagedList<dm_KhachHang> GetKhachHangPagedList(int? id)// id = page Index
        {
            return DBDanhMucChung.dm_KhachHangs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_KhachHang BuildNewKhachHang(KhachHangForm form)
        {
            dm_KhachHang newKhachHang = new dm_KhachHang();
            newKhachHang.TenKH = form.TenKH;
            newKhachHang.DiaChi = form.DiaChi;
            newKhachHang.SoDT = form.SoDT;
            newKhachHang.Fax = form.Fax;
            newKhachHang.Email = form.Email;
            newKhachHang.WebSite = form.WebSite;
            newKhachHang.MaST = form.MaST;
            newKhachHang.IdLoaiKhachHang = form.IdLoaiKhachHang;
            return newKhachHang;
        }
        public dm_KhachHang BuildEditKhachHang(KhachHangForm form)
        {
            dm_KhachHang newKhachHang = new dm_KhachHang();
            newKhachHang.Id = form.Id;
            newKhachHang.TenKH = form.TenKH;
            newKhachHang.DiaChi = form.DiaChi;
            newKhachHang.SoDT = form.SoDT;
            newKhachHang.Fax = form.Fax;
            newKhachHang.Email = form.Email;
            newKhachHang.WebSite = form.WebSite;
            newKhachHang.MaST = form.MaST;
            newKhachHang.IdLoaiKhachHang = form.IdLoaiKhachHang;
            return newKhachHang;
        }
        public dm_KhachHang GetKhachHangIdMax()
        {
            IList<dm_KhachHang> list = DBDanhMucChung.dm_KhachHangs.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public SelectList BuilKhachHang(int? selectedValue,int Loaikh)
        {
            IList<dm_KhachHang> selectKhachHang = DBDanhMucChung.dm_KhachHangs.Where(p=>p.IdLoaiKhachHang==Loaikh).OrderBy(p => p.Id).ToList();
            IList<object> selectList = new List<object>();
            int dem = 0;
            foreach (var o in selectKhachHang)
            {
                dem++;
                if (selectedValue != 0 || dem == selectedValue)
                {
                    selectList.Add(new { value = o.Id, Text = o.TenKH, SelectedValue = selectedValue });
                }
                else
                {
                    selectList.Add(new { value = o.Id, Text = o.TenKH });
                }
            }
            SelectList select = new SelectList(selectList, "Value", "Text", selectedValue);
            return select;
        }
        public SelectList BuilLoaiKhachHang(int? selectedValue)
        {
            IList<dm_LoaiKhachHang> selectKhachHang = DBDanhMucChung.dm_LoaiKhachHangs.OrderBy(p => p.Id).ToList();
            IList<object> selectList = new List<object>();
            int dem = 0;
            foreach (var o in selectKhachHang)
            {
                dem++;
                if (selectedValue != 0 || dem == selectedValue)
                {
                    selectList.Add(new { value = o.Id, Text = o.Ten, SelectedValue = selectedValue });
                }
                else
                {
                    selectList.Add(new { value = o.Id, Text = o.Ten });
                }
            }
            SelectList select = new SelectList(selectList, "Value", "Text", selectedValue);
            return select;
        }
    }
}